package com.mask.im.business.controller.manage;

import com.mask.im.business.annotation.AccessBarrier;
import com.mask.im.business.annotation.OptLog;
import com.mask.im.business.common.model.PageResult;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.model.dto.UserOnlineDTO;
import com.mask.im.business.model.vo.ConditionVO;
import com.mask.im.business.model.vo.UserDisableVO;
import com.mask.im.business.model.vo.UserRoleVO;
import com.mask.im.business.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import static com.mask.im.business.common.constant.OptTypeConstant.DELETE;
import static com.mask.im.business.common.constant.OptTypeConstant.UPDATE;

/**
 * @author jian
 */
@Api(tags = "用户管理模块")
@RestController
public class SysUserController {

	@Autowired
	private UserInfoService userInfoService;


	@OptLog(optType = UPDATE)
	@ApiOperation(value = "修改用户角色")
	@PutMapping("/admin/users/role")
	public RestResponse<?> updateUserRole(@Valid @RequestBody UserRoleVO userRoleVO) {
		userInfoService.updateUserRole(userRoleVO);
		return RestResponse.ok();
	}

	@OptLog(optType = UPDATE)
	@ApiOperation(value = "修改用户禁用状态")
	@PutMapping("/admin/users/disable")
	public RestResponse<?> updateUserDisable(@Valid @RequestBody UserDisableVO userDisableVO) {
		userInfoService.updateUserDisable(userDisableVO);
		return RestResponse.ok();
	}

	@ApiOperation(value = "查看在线用户")
	@GetMapping("/admin/users/online")
	public RestResponse<PageResult<UserOnlineDTO>> listOnlineUsers(ConditionVO conditionVO) {
		return RestResponse.ok(userInfoService.listOnlineUsers(conditionVO));
	}

	@OptLog(optType = DELETE)
	@ApiOperation(value = "下线用户")
	@DeleteMapping("/admin/users/{userInfoId}/online")
	public RestResponse<?> removeOnlineUser(@PathVariable("userInfoId") Integer userInfoId) {
		userInfoService.removeOnlineUser(userInfoId);
		return RestResponse.ok();
	}

	@OptLog(optType = DELETE)
	@AccessBarrier(size = 2,limitTime = 60*60*24)
	@ApiOperation(value = "下线全部用户")
	@DeleteMapping("/admin/users/allOnline")
	public RestResponse<?> removeAllOnlineUser(@RequestParam(value = "reason",defaultValue = "无理由") String reason) {
		userInfoService.removeAllOnlineUser(reason);
		return RestResponse.ok();
	}
}
